package com.zff.rental.validation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.Objects;

/**
 * @author HCY
 */
public class StringInValidator implements ConstraintValidator<StringIn, String> {
    private String[] in;

    @Override
    public void initialize(StringIn constraintAnnotation) {
        in = constraintAnnotation.in();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (in == null && value == null) {
            return true;
        }
        if (in == null) {
            return false;
        }
        for (String v : in) {
            if (Objects.equals(v, value)) {
                return true;
            }
        }
        return false;
    }
}
